/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package polandballgame;

import javax.swing.*;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.Color;


/**
 *
 * @author POed
 */
public class GameView extends JPanel implements KeyListener {
    
    private enum Action {
        
        ActionNothing,
        ActionMove,
        ActionLoad
    }
    
    // data:
    private Engine engine;
    
    public GameView() {
        
        // appearence
        this.setBackground(Color.white);
    }
    
    @Override public void keyTyped(KeyEvent event) {}
    @Override public void keyReleased(KeyEvent event) {}
    @Override public void keyPressed(KeyEvent event) {
        
        if (engine == null) {
            return;
        }
        
        Action action = Action.ActionNothing;
        Engine.Direction direction = Engine.Direction.DirectionUp;
        String mapName = "maps/map_0.pbm";
        
        switch (event.getKeyCode()) {
            
            case KeyEvent.VK_UP:
                direction = Engine.Direction.DirectionUp;
                action = Action.ActionMove;
                break;
                
            case KeyEvent.VK_RIGHT:
                direction = Engine.Direction.DirectionRight;
                action = Action.ActionMove;
                break;
                
            case KeyEvent.VK_DOWN:
                direction = Engine.Direction.DirectionDown;
                action = Action.ActionMove;
                break;
                
            case KeyEvent.VK_LEFT:
                direction = Engine.Direction.DirectionLeft;
                action = Action.ActionMove;
                break;
                
            case KeyEvent.VK_1:
                mapName = "maps/map_0.pbm";
                action = Action.ActionLoad;
                break;
                
            case KeyEvent.VK_2:
                mapName = "maps/map_1.pbm";
                action = Action.ActionLoad;
                break;
                
            default:
                break;
        }
        
        switch (action) {
            
            case ActionMove:
                engine.moveHero(direction);
                break;
            case ActionLoad:
                engine.loadData(mapName);
                break;
            case ActionNothing:
            default:
                break;
        }
    }
    
    
    // methods:
    public void setEngine(Engine anEngine) {
        
        engine = anEngine;
    }
    
    @Override public void paintComponent(Graphics context) {
        
        if (engine != null) {
            engine.draw(context);
        }
    }
}
